package com.arcblock.demo


import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.text.TextUtils
import android.util.Log
import android.view.Window
import android.view.WindowManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.arcblock.demo.adapter.MainAdapter
import com.arcblock.demo.constant.Constant.Companion.BASE_URL
import com.arcblock.demo.inter.IGetDataInterface
import com.arcblock.demo.entity.Frontmatter
import com.arcblock.demo.inter.IOnItemClickListener
import com.arcblock.demo.present.MainPresenter

/**
 * 采用mvp模式
 */
class MainActivity : AppCompatActivity(), IGetDataInterface,IOnItemClickListener {
    lateinit var presenter: MainPresenter
    var list = ArrayList<Frontmatter>()
    lateinit var adapter: MainAdapter

    //onCreate方法
    override fun onCreate(savedInstanceState: Bundle?) {
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE); //去除title
        super.onCreate(savedInstanceState)
        requestWindowFeature(Window.FEATURE_NO_TITLE)
        window.addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN)
        setContentView(R.layout.activity_main)
        initView()
        presenter = MainPresenter(this)
        presenter.getMainInfo()
    }

    //view初始化
    private fun initView() {
        val mRecyclerView = findViewById<RecyclerView>(R.id.recycler_view)
        val layoutManager = LinearLayoutManager(this)
        layoutManager.orientation = LinearLayoutManager.VERTICAL
        mRecyclerView.layoutManager = layoutManager
        adapter = MainAdapter(list,this,this)
        mRecyclerView.adapter = adapter
    }

    //网络访问成功回调
    override fun onMainSuccess(datas: List<Frontmatter>) {
        Log.e("onMainSuccess","onMainSuccess")
        list.addAll(datas)
        adapter.notifyDataSetChanged()
    }
    //网络访问失败回调
    override fun onMainFailed() {
        Log.e("onMainFailed","onMainFailed")
    }

    //item点击事件回调
    override fun onItemClick(url: String) {
        if(TextUtils.isEmpty(url)){
            return;
        }
        //跳转webview页面
        val intent = Intent()
        //获取intent对象
        intent.setClass(this,WebViewActivity::class.java)
        var path = BASE_URL+url
        intent.putExtra("url",path)
        startActivity(intent)
    }


}